#include "my_service.h"

#include <iostream>
#include <mutex>

#include "my_socket_client.h"

sptr<MyService> MyService::GetInstance()
{
    if (instance == nullptr) {
        static std::mutex mutex;
        std::lock_guard<std::mutex> lock(mutex);
        if (instance == nullptr) {
            instance = new MyService();
        }
    }
    return instance;
}

std::string MyService::Call(int id, const std::string message)
{
    auto socket = MySocketClient::GetInstance();
    {
        auto ret = socket->Connect("127.0.0.1", 55555);
        std::cout << "Connect: " << ret << ", errno: " << errno << std::endl;
        if (ret) {
            return "connect failed";
        }
    }
    struct Message m = {
        .id = id,
        .message = message,
    };
    socket->Send(m);
    auto ret = socket->Recv();
    socket->Disconnect();
    return ret;
}
